#include "CThread.h"
#include <iostream>
#include <errno.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <deque>
#include <signal.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <map>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <vector>
#include <assert.h>
#include <string.h>
#include <stdlib.h>

CThread::CThread() : m_pid(0)
{
	maskSIGUSR1();
	sigemptyset(&m_waitSig);
	sigaddset(&m_waitSig, SIGUSR1);
}

CThread::~CThread()
{
}

void CThread::maskSIGUSR1()
{
	sigset_t sig;
	sigemptyset(&sig);
	sigaddset(&sig , SIGUSR1);
	pthread_sigmask(SIG_BLOCK , &sig , NULL);
}

pthread_t CThread::getThreadId()
{
	return m_pid;
}

void *CThread::threadFun(void *arg)
{
	CThread *pThread = static_cast<CThread*>(arg);
	pThread->Run();
}

bool CThread::Start()
{
	int nRet = pthread_create(&m_pid , NULL , threadFun , this);
	if(0 == nRet)
	{
		nRet = pthread_detach(m_pid);  ///线程为分离状态 ,线程退出的时候自动释放资源,不用调用pthread_join
		if(nRet != 0)
		{
		
			return false;
		}
	}
	else
	{
		
		return false;
	}
	return true;
}

void CThread::pause()
{
	int sig;
	sigwait(&m_waitSig , &sig);
}

void CThread::continues()
{
	pthread_kill(m_pid , SIGUSR1);
}
